/*
 * Copyright 1999-2011 Alibaba Group.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.dubbo.common.logger.log4j2;

import java.io.File;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.FileAppender;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.LoggerConfig;

import com.alibaba.dubbo.common.logger.Level;
import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerAdapter;

public class Log4j2LoggerAdapter implements LoggerAdapter {

    private File file;

    public Log4j2LoggerAdapter() {
        try {
            final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
            final Configuration config = ctx.getConfiguration();
            LoggerConfig loggerConfig = config.getRootLogger();
            Map<String, Appender> appenders = loggerConfig.getAppenders();
            for (Map.Entry<String, Appender> entry : appenders.entrySet()) {
                Appender appender = entry.getValue();
                if (appender instanceof FileAppender) {
                    FileAppender fileAppender = (FileAppender) appender;
                    String filename = fileAppender.getFileName();
                    file = new File(filename);
                    break;
                }
            }
        } catch (Throwable t) {
        }
    }

    @Override
    public Logger getLogger(Class<?> key) {
        return new Log4j2Logger(LogManager.getLogger(key));
    }

    @Override
    public Logger getLogger(String key) {
        return new Log4j2Logger(LogManager.getLogger(key));
    }

    @Override
    public void setLevel(Level level) {
        org.apache.logging.log4j.Level lvl = org.apache.logging.log4j.Level.OFF;
        if (level == Level.ALL)
            lvl = org.apache.logging.log4j.Level.ALL;
        if (level == Level.TRACE)
            lvl = org.apache.logging.log4j.Level.TRACE;
        if (level == Level.DEBUG)
            lvl = org.apache.logging.log4j.Level.DEBUG;
        if (level == Level.INFO)
            lvl = org.apache.logging.log4j.Level.INFO;
        if (level == Level.WARN)
            lvl = org.apache.logging.log4j.Level.WARN;
        if (level == Level.ERROR)
            lvl = org.apache.logging.log4j.Level.ERROR;

        final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
        final Configuration config = ctx.getConfiguration();
        LoggerConfig loggerConfig = config.getRootLogger();
        loggerConfig.setLevel(lvl);
        ctx.updateLoggers();
    }

    @Override
    public Level getLevel() {
        org.apache.logging.log4j.Level level = LogManager.getRootLogger().getLevel();
        if (level == org.apache.logging.log4j.Level.ALL)
            return Level.ALL;
        if (level == org.apache.logging.log4j.Level.TRACE)
            return Level.TRACE;
        if (level == org.apache.logging.log4j.Level.DEBUG)
            return Level.DEBUG;
        if (level == org.apache.logging.log4j.Level.INFO)
            return Level.INFO;
        if (level == org.apache.logging.log4j.Level.WARN)
            return Level.WARN;
        if (level == org.apache.logging.log4j.Level.ERROR)
            return Level.ERROR;
        // if (level == org.apache.logging.log4j.Level.OFF)
        return Level.OFF;
    }

    @Override
    public File getFile() {
        return file;
    }

    @Override
    public void setFile(File file) {
    }

}
